package com.example.todo;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class main extends Activity implements OnClickListener, OnKeyListener {
    
	EditText txtItem;
	Button bttnAdd;
	ListView listItems;
	
	//declare storage variables
	ArrayList<String> toDoItems;
	ArrayAdapter<String> aa;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //instantiate objects
        txtItem = (EditText)findViewById(R.id.txtItem);
        bttnAdd = (Button)findViewById(R.id.btnAdd);
        listItems = (ListView)findViewById(R.id.listItems);
        
        //set listeners
        bttnAdd.setOnClickListener(this);
        txtItem.setOnKeyListener(this);
        
        //instantiate some more
        toDoItems = new ArrayList<String>();
        aa = new ArrayAdapter(this,android.R.layout.simple_list_item_1,toDoItems);
        listItems.setAdapter(aa);
        registerForContextMenu( listItems );
        /*listItems.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				deleteSelectedItem( arg2 );
				return true;
			}
        });*/
    }
    

    public void onCreateContextMenu(ContextMenu menu, View v,
    	    ContextMenuInfo menuInfo) {
    	  if (v.getId()==R.id.listItems) {
    	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    	    menu.setHeaderTitle("Options");
    	    String [] menuOptions = getResources().getStringArray(R.array.item_menu);
    	    for ( int i = 0; i < menuOptions.length; i++ ){
    	    	 menu.add(Menu.NONE, i, i, menuOptions[i] );
    	    }
    	  }
    	} 
    public boolean onContextItemSelected(MenuItem item) {
    	//get info about the todo item selected
    	AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    	//get the menu option selected
    	int menuItemIndex = item.getItemId();
    	String[] menuItems = getResources().getStringArray(R.array.item_menu);
    	String menuItemName = menuItems[menuItemIndex];
  
    	if( menuItemName.equals( "Delete") ){
    		deleteSelectedItem( info.position );
    	}else if( menuItemName.equals("Edit" ) ){
    		info.
    	}
    	return true;
    }
    
    public setEditable( int position ){
    	this.listItems[position].
    }
    //deletes a todo item at @position
    public void deleteSelectedItem( int position ){
		this.deleteItem( position );
    }
    
   /* public boolean onCreateOptionsMenu( Menu menu ){
    	super.onCreateOptionsMenu( menu );
    	MenuItem item;
    	item = menu.add("delete");
    	item.setIcon( R.drawable.trash_icon );
    	return true;
    }
    
    public boolean onOptionsItemSelected( MenuItem item ){
    	super.onOptionsItemSelected( item );
    	if( item.getTitle() == "delete"){
    		int index = listItems.getSelectedItemPosition();
    		this.deleteItem( index );
    	}
    	return true;
    }*/
    private void addItem( String item ){
    	if(item.length() > 0 ){
    		Toast.makeText(this.getApplicationContext(), item+"added",Toast.LENGTH_SHORT).show();
    		this.toDoItems.add(item);
    		this.aa.notifyDataSetChanged();
    		this.txtItem.setText("");
    	}
    }
    
    private void deleteItem( int itemId ){
    	if( itemId>=0 ){
    		String itemName = (String)listItems.getItemAtPosition( itemId );
    		Toast.makeText(this.getApplicationContext(), itemName+"deleted",Toast.LENGTH_SHORT).show();
    		this.toDoItems.remove( itemId );
    		aa.notifyDataSetChanged();
    	}
    }
	public void onClick(View v) {
		if( v == this.bttnAdd ){
			this.addItem(this.txtItem.getText().toString());
		}
	}

	public boolean onKey(View view, int keyCode, KeyEvent event) {
		if( event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
			this.addItem(this.txtItem.getText().toString());
		}
		return false;
	}
}